package fr.univ.lille1.android.fragments;

import java.util.HashMap;
import java.util.Map;

import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;
import com.googlecode.androidannotations.annotations.SystemService;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;

import fr.univ.lille1.android.R;

@EFragment(R.layout.fragment_add_comment)
@OptionsMenu(R.menu.menu_add_comment)
public class AddCommentFragment extends SherlockFragment {
	private static final String SEND_TITLE = "send_title";
	private static final String SEND_COMMENT = "send_comment";
	private static final String SEND_GPS = "send gps";
	private static final String SEND_ID = "send id";

	private boolean mSendComment = false;

	@ViewById(R.id.editTitle)
	EditText mEditTitle;
	@ViewById(R.id.editComment)
	EditText mEditComment;
	
	@SystemService
	InputMethodManager mInputMethodManager;

	@OptionsItem(R.id.menu_send)
	void menuSendComment() {
		trySendComment();
	}

	/**
	 * Try to connect with the tag this form.
	 */
	public void trySendComment() {
		if (mSendComment) {
			return;
		}
		mEditTitle.setError(null);
		mEditComment.setError(null);

		boolean cancel = false;
		View focusView = null;

		final String title = mEditTitle.getText().toString();
		final String comment = mEditComment.getText().toString();

		// User must fill all fields.
		if (TextUtils.isEmpty(title)) {
			mEditTitle.setError(getString(R.string.error_message_no_value));
			cancel = true;
			focusView = mEditTitle;
		} else if (TextUtils.isEmpty(comment)) {
			mEditComment.setError(getString(R.string.error_message_no_value));
			cancel = true;
			focusView = mEditComment;
		}
		// If there is one field empty, we indicate at the user. Otherwise, we
		// launch the connection request.
		if (cancel) {
			focusView.requestFocus();
		} else {
			getSherlockActivity().setProgressBarIndeterminateVisibility(true);
			mSendComment = true;
			final Map<String, String> params = new HashMap<String, String>();
			params.put(SEND_TITLE, mEditTitle.getText().toString());
			params.put(SEND_COMMENT, mEditComment.getText().toString());
			params.put(SEND_GPS, "GPS_VALUE");
			params.put(SEND_ID, "ID_USER");
			sendCommentOnServer(params);
		}
	}

	@Background
	void sendCommentOnServer(final Map<String, String> params) {
		// TODO Here, we call API from the other maven projects to send comment
		// of the current user
		onPostExecute();
	}

	@UiThread
	void onPostExecute() {
		mSendComment = false;
		mInputMethodManager.hideSoftInputFromWindow(mEditTitle.getWindowToken(), 0);
		mInputMethodManager.hideSoftInputFromWindow(mEditComment.getWindowToken(), 0);
		getFragmentManager().popBackStack();
	}
}
